package activity;

import java.util.HashMap;
import java.util.Map;

import com.activeandroid.util.Log;
import com.android.volley.NetworkResponse;
import com.android.volley.VolleyError;
import com.qzhy.female.R;

import Constant.Constance;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import model.SignInfo;
import model.Sign_status;
import model.UserSign;
import utils.GsonUtils;
import utils.HTTPUtils;
import utils.SetUtils;
import volley.VolleyListener;

public class SignInActivity extends ActionBarActivity implements OnClickListener {

	private ImageView mImgBack;
	private Button mBtnSignIn;
	private int money;
//	private ArrayList<Sign> DataSign=new ArrayList<Sign>();
//	private ArrayList<User> DataUser=new ArrayList<User>();
	private TextView mTvShowSign;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sign_in);
		InitUI();
		InitSignState();
	}

	private void InitSignState() {
		
//		OkHttpClientManager.getInstance()
		
		
		HTTPUtils.get(getApplicationContext(), Constance.URL.USER+"/"+SetUtils.GetUserId(getApplicationContext()), new VolleyListener() {
			private int statusCode;
			@SuppressLint("ResourceAsColor")
			@Override
			public void onResponse(String arg0) {
				Log.e("json返回数据", arg0.toString());
//				Toast.makeText(getApplicationContext(), arg0.toString(), Toast.LENGTH_SHORT).show();
				Sign_status sign = GsonUtils.parseJSON(arg0, Sign_status.class);
				String status = sign.getStatus();
				UserSign result = sign.getResult();
				SignInfo usersign = result.getSign();
				if(status.equals("error")){
					Toast.makeText(getApplicationContext(), "您已经签到过", Toast.LENGTH_SHORT).show();
					mTvShowSign.setText("您的积分："+usersign.getSignCount()+"提示今天您已签到过");
					mBtnSignIn.setBackgroundColor(R.color.red);
					mBtnSignIn.setEnabled(false);
					
				}else {
//					Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT).show();
//					Toast.makeText(getApplicationContext(), "恭喜您，签到成功，获得积分1", Toast.LENGTH_SHORT).show();
					mTvShowSign.setText("您的积分："+usersign.getSignCount());
				}
				
			}
			@Override
			public void onErrorResponse(VolleyError arg0) {
			}
		});
	}

	private void InitUI() {
		mImgBack = (ImageView) findViewById(R.id.img_SignBack);
		mImgBack.setOnClickListener(this);
		
		mBtnSignIn = (Button) findViewById(R.id.btn_signs);
		mBtnSignIn.setOnClickListener(this);
		
		mTvShowSign = (TextView) findViewById(R.id.tv_showsign);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.img_SignBack:
			finish();
			break;
		case R.id.btn_signs:
			UserSign();
			break;
		default:
			break;
		}
	}

	private void UserSign() {
		android.util.Log.e("执行signin","执行signin");
		Map<String, String> params=new HashMap<String,String>();
		params.put("id", ""+SetUtils.GetUserId(getApplicationContext()));
		utils.HTTPUtils.post(getApplicationContext(),  Constance.URL.USERSIGNIN, params, new volley.VolleyListener() {
			private int statusCode;
			@Override
			public void onResponse(String arg0) {
				Log.e("json返回数据", arg0.toString());
//					Toast.makeText(getApplicationContext(), arg0.toString(), Toast.LENGTH_SHORT).show();
				Sign_status sign = GsonUtils.parseJSON(arg0, Sign_status.class);
				String status = sign.getStatus();
				UserSign result = sign.getResult();
				SignInfo usersign = result.getSign();
				if(status.equals("error")){
					Toast.makeText(getApplicationContext(), "您已经签到过", Toast.LENGTH_SHORT).show();
					mTvShowSign.setText("您的积分："+usersign.getSignCount()+"提示今天您已签到过");
				}else {
//						Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT).show();
					mTvShowSign.setText("您的积分："+usersign.getSignCount());
				}
				
			}
			@Override
			public void onErrorResponse(VolleyError arg0) {
				Toast.makeText(getApplicationContext(), "onErrorResponse", Toast.LENGTH_SHORT).show();
				NetworkResponse networkResponse = arg0.networkResponse;
				statusCode = networkResponse.statusCode;
				
				
				mTvShowSign.setText("用户已经签到："+statusCode+"安卓当前时间");
				if(statusCode==Constance.STATUCODE.SIGINED){
					mBtnSignIn.setEnabled(false);
				}else {
					mBtnSignIn.setEnabled(true);
				}
			}
		});
	}
}
